package com.atguigu.gmall.product.controller;


import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.model.BaseTrademark;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.message.ReusableMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.ReactiveSubscription;
import org.springframework.web.bind.annotation.*;
import com.atguigu.gmall.product.service.BaseTrademarkService;

import org.springframework.web.bind.annotation.RestController;

/**
 * 品牌表 前端控制器
 *
 * @author atguigu
 * @since 2023-08-30
 */
@Api(tags = "品牌表控制器")
@RestController
@RequestMapping("/admin/product")
public class BaseTrademarkController {

    @Autowired
    private BaseTrademarkService baseTrademarkService;


    /**
     * 分页查询所有品牌
     *
     * @param page
     * @param page
     * @return
     */
    @ApiOperation("分页查询所有品牌")
    @GetMapping("/baseTrademark/{page}/{limit}")
    public Result getTrademarkByPage(@PathVariable("page") Integer page, @PathVariable("limit") Integer limit) {

        //1.构建分页对象(封装页码跟页大小)
        Page<BaseTrademark> pageInfo = new Page<>(page, limit);
        //2.调用业务层提供分页方法 page方法(分页对象, 查询条件)  - 执行完业务层分页方法后，封装分页对象中（总数，页数，当前页记录）
        pageInfo = baseTrademarkService.getTrademarkByPage(pageInfo);
        return Result.ok(pageInfo);
    }

    /**
     * 新增品牌
     * @param baseTrademark
     * @return
     */
    @ApiOperation("新增品牌")
    @PostMapping("/baseTrademark/save")
    public Result saveTrademark(@RequestBody BaseTrademark baseTrademark){
        baseTrademarkService.save(baseTrademark);
        return Result.ok();
    }

    /**
     * 新增品牌
     * @param baseTrademark
     * @return
     */
    @ApiOperation("修改品牌")
    @PutMapping("/baseTrademark/update")
    public Result updateTrademark(@RequestBody BaseTrademark baseTrademark){
        baseTrademarkService.updateById(baseTrademark);
        return Result.ok();
    }

    /**
     * 根据品牌Id 获取品牌对象
     * @param id
     * @return
     */
    @ApiOperation("根据品牌Id 获取品牌对象")
    @GetMapping("/baseTrademark/get/{id}")
    public Result getTradeById(@PathVariable("id") Long id){
        BaseTrademark trademark = baseTrademarkService.getById(id);
        return Result.ok(trademark);
    }

    /**
     * 删除品牌
     * @param id
     * @return
     */
    @ApiOperation("删除品牌")
    @DeleteMapping("/baseTrademark/remove/{id}")
    public Result deleteById(@PathVariable("id") Long id){
        baseTrademarkService.removeById(id);
        return Result.ok();
    }

}
